package com.an.vhr.controller.system.basic;

import com.an.vhr.bean.Menu;
import com.an.vhr.bean.RespBean;
import com.an.vhr.bean.Role;
import com.an.vhr.mapper.MenuRoleMapper;
import com.an.vhr.service.MenuRoleService;
import com.an.vhr.service.MenuService;
import com.an.vhr.service.PremissService;
import com.an.vhr.service.RoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: an1089
 **/
@RestController
@RequestMapping("/system/basic/premiss/")
public class PremissController {
    @Resource
    PremissService premissService;

    @Resource
    MenuService menuService;

    @Resource
    RoleService roleService;

    @Resource
    MenuRoleService menuRoleService;

    @GetMapping("/")
    public List<Role> getALlRoles(){
        return roleService.getALlRoles();
    }

    @GetMapping("/menus")
    public List<Menu> getAllMenu(){
        return menuService.getAllMenu();
    }

    @GetMapping("/mids/{rid}")
    public List<Integer> getMidsByRid(@PathVariable Integer rid){
        return menuService.getMidsByRid(rid);
    }

    @PutMapping("/")
    public RespBean updateMenuRole(Integer rid, Integer[] mids){
        if(menuService.updateMenuRole(rid,mids)){
            return RespBean.ok("更新成功");
        }
        return RespBean.error("更新失败");
    }

    @PostMapping("/")
    public RespBean addRole(@RequestBody Role role){
        if(roleService.addRole(role) == 1){
            return RespBean.ok("添加成功");
        }
        return RespBean.error("添加失败");
    }

    @DeleteMapping("/{id}")
    public RespBean deleteRole(@PathVariable Integer id){
        menuRoleService.deleteMenuRoleByRid(id);
        if(roleService.deleteRole(id) == 1){
            return RespBean.ok("删除成功");
        }
        return RespBean.error("删除失败");
    }
}
